package cn.faster.sys.controller;

import cn.faster.common.enums.CodeEnum;
import cn.faster.common.result.ResultBean;
import cn.faster.sys.entity.SysUser;
import cn.faster.sys.service.SysUserService;
import cn.faster.sys.vo.SysUserVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xiaoCoder
 * @date 2021-06-06
 */
@RestController
@CrossOrigin
@AllArgsConstructor
@Api(value = "用户表接口", tags = "用户表接口")
@RequestMapping("/sys/user")
@RequiresRoles("admin")
public class SysUserController {

    private final SysUserService sysUserService;

    @ApiOperation(value = "获取user根据id", notes = "")
    @GetMapping("/getById/{id}")
    //@RequiresMenus({"teacher"})
    public ResultBean<?> user(@PathVariable Integer id) {
        return ResultBean.SUCCESS("获取user信息成功！", sysUserService.getById(id));
    }

    @ApiOperation(value = "获取user列表", notes = "")
    @GetMapping("")
    //@RequiresMenus({"teacher"})
    public ResultBean<?> userList(SysUser sysUser) {
        return ResultBean.SUCCESS("获取user信息成功", sysUserService.getUserList(sysUser));
    }

    @ApiOperation(value = "获取user列表", notes = "")
    @GetMapping("getUserListByPage")
    //@RequiresMenus({"teacher"})
    public ResultBean<?> userList(SysUser sysUser,Page<SysUser> sysUserPage) {
        return ResultBean.SUCCESS("获取user信息成功", sysUserService.getUserListByPage(sysUser,sysUserPage));
    }


    @ApiOperation(value = "添加user", notes = "新增一条user")
    @PostMapping("")
    //@RequiresMenus({"teacher"})
    public ResultBean<?> userAdd(SysUserVO userVO) throws Exception {
        return ResultBean.SUCCESS("新增一条user成功！", sysUserService.save(userVO));
    }

    @ApiOperation(value = "修改user", notes = "根据id修改user")
    @PutMapping("")
    //@RequiresMenus({"teacher"})
    public ResultBean<?> userUpdate(SysUserVO userVO) throws Exception {
        if (StringUtils.isEmpty(userVO.getId())) {
            return ResultBean.VALIDATE(CodeEnum.PARAMETER_FORMAT_DOES_NOT_MATCH);
        }
        return ResultBean.SUCCESS("修改一条user成功！", sysUserService.updateById(userVO));
    }

    @ApiOperation(value = "逻辑删除user", notes = "根据id逻辑删除user")
    @DeleteMapping("/removeById/{id}")
    //@RequiresMenus({"teacher"})
    public ResultBean<?> userRemove(@PathVariable("id") Integer id) {
        return ResultBean.SUCCESS("删除一条user成功！", sysUserService.removeById(id));
    }

    @ApiOperation(value = "获取用户权限",notes="获取用户权限")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @PostMapping("/getUserRole")
    public ResultBean<?> getUserRole(@RequestParam(value = "id") Integer id) throws Exception {

        SysUserVO sysUserInfo = sysUserService.getUserRole(id);
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.setData(sysUserInfo);
        return resultBean;
    }
}
